/*
 * AchievementMenuItem.cpp
 *
 *  Created on: 2015-9-28
 *      Author: 10095427
 */

#include "AchievementMenuItem.h"
#include "LocalizedStringGetter.h"

#define WIDTH   310
#define HEIGHT  75
#define LEVEL_FONT_SIZE  16
#define LEVEL_FONT_NAME  "arial"
#define LEVEL_X          2
#define LEVEL_Y          (HEIGHT - 20)
#define TITLE_FONT_SIZE  15
#define TITLE_FONT_NAME  "arial"
#define TITLE_X          2
#define TITLE_Y          (LEVEL_Y - 16)
#define INFO_FONT_SIZE   12
#define INFO_FONT_NAME   "arial"
#define INFO_WIDTH       160
#define INFO_HEIGHT      40
#define INFO_X           2
#define INFO_Y           0
#define PROGRESS_WIDTH   140
#define PROGRESS_HEIGHT  15
#define PROGRESS_X       (WIDTH - PROGRESS_WIDTH - 4)
#define PROGRESS_Y       (HEIGHT - PROGRESS_HEIGHT - 4)
#define PROGRESS_FONT_SIZE   13
#define PROGRESS_FONT_NAME   "arial"
#define REWARD_TITLE_X       PROGRESS_X
#define REWARD_TITLE_Y       8

typedef enum {
	BgTag=10,
    LevelInfoTag,
    InfoTitleTag,
    InfoTag,
    ProgressTag,
    ProgressBgTag,
    ProgressTextTag,
    DiamondRewardTitleTag,
    DiamondRewardNumTag,
    DiamondRewardIconTag,



} MenuItemTag;

typedef enum {
	ZBg,
    ZText,
    ZProgress,
    ZProgressNum,

} ZIndex;

AchievementMenuItem::AchievementMenuItem() {

}

AchievementMenuItem::~AchievementMenuItem() {
}

AchievementMenuItem * AchievementMenuItem::create(const std::string& normalImage, const std::string& selectedImage, const std::string& disabledImage, const ccMenuCallback& callback)
{
    return (AchievementMenuItem *)MenuItemImage::create(normalImage, selectedImage, disabledImage, callback);
}

void AchievementMenuItem::setLevelInfo(const std::string& text)
{
	if(getChildByTag(LevelInfoTag)){
		removeChildByTag(LevelInfoTag);
	}
    LabelTTF *label = LabelTTF::create(text, LEVEL_FONT_NAME, LEVEL_FONT_SIZE);
    label->setAnchorPoint(Vec2::ZERO);
    label->setPosition(LEVEL_X,LEVEL_Y);
    label->setColor(Color3B::YELLOW);
    addChild(label,ZText,LevelInfoTag);

}

void AchievementMenuItem::setInfoTitle(const std::string& text)
{
	if(getChildByTag(InfoTitleTag)){
		removeChildByTag(InfoTitleTag);
	}
    LabelTTF *label = LabelTTF::create(text, TITLE_FONT_NAME, TITLE_FONT_SIZE);
    label->setAnchorPoint(Vec2::ZERO);
    label->setPosition(TITLE_X,TITLE_Y);
    label->setColor(Color3B::WHITE);
    addChild(label,ZText,InfoTitleTag);

}

void AchievementMenuItem::setInfo(const std::string& text)
{
	if(getChildByTag(InfoTag)){
		removeChildByTag(InfoTag);
	}
    LabelTTF *label = LabelTTF::create(text,INFO_FONT_NAME ,INFO_FONT_SIZE,Size(INFO_WIDTH,INFO_HEIGHT),TextHAlignment::LEFT);
    label->setAnchorPoint(Vec2::ZERO);
    label->setPosition(INFO_X,INFO_Y);
    label->setColor(Color3B::BLACK);
    addChild(label,ZText,InfoTag);
}

void AchievementMenuItem::setProgressState(int nowNum, int maxNum)
{
    auto cache = SpriteFrameCache::getInstance();
	cache->addSpriteFramesWithFile("CommonIcon.plist");
    auto cache1 = SpriteFrameCache::getInstance();
	cache1->addSpriteFramesWithFile("Diamond.plist");
    auto cache2 = SpriteFrameCache::getInstance();
	cache2->addSpriteFramesWithFile("Achieves.plist");

	if(getChildByTag(ProgressBgTag)){
		removeChildByTag(ProgressTag);
		removeChildByTag(ProgressBgTag);
		removeChildByTag(ProgressTextTag);
	}

	auto sprite1 = Sprite::createWithSpriteFrameName("AchieveProcessBar.png");
	ProgressTimer *progress = ProgressTimer::create(sprite1);
	progress->setAnchorPoint(Vec2::ZERO);
	progress->setPosition(PROGRESS_X,PROGRESS_Y);
    addChild(progress,ZProgress,ProgressTag);
    progress->setType(ProgressTimer::Type::BAR);
    progress->setMidpoint(Point(0,0));
    progress->setBarChangeRate(Point(1,0));
    progress->setPercentage(100 * nowNum / maxNum);

    auto progressBg = Sprite::createWithSpriteFrameName("AchieveProcessBg.png");
    progressBg->setAnchorPoint(Vec2::ZERO);
    progressBg->setPosition(PROGRESS_X,PROGRESS_Y);
    addChild(progressBg,ZText,ProgressBgTag);


    LabelTTF *label = LabelTTF::create(__String::createWithFormat("%d/%d",nowNum,maxNum)->getCString(), PROGRESS_FONT_NAME, PROGRESS_FONT_SIZE);
    label->setAnchorPoint(Vec2::ZERO);
    label->setPosition(PROGRESS_X+(PROGRESS_WIDTH-label->getContentSize().width)/2,PROGRESS_Y);
    label->setColor(Color3B::WHITE);
    addChild(label,ZProgressNum,ProgressTextTag);
}

void AchievementMenuItem::setRewardDiamond(int diamondNum)
{
	if(getChildByTag(DiamondRewardTitleTag)){
		removeChildByTag(DiamondRewardTitleTag);
		removeChildByTag(DiamondRewardNumTag);
		removeChildByTag(DiamondRewardIconTag);
	}
    LabelTTF *label = LabelTTF::create(LocalizedStringGetter::getStringByKey("AchieveRewardString"), TITLE_FONT_NAME, TITLE_FONT_SIZE);
    label->setAnchorPoint(Vec2::ZERO);
    label->setPosition(REWARD_TITLE_X,REWARD_TITLE_Y);
    label->setColor(Color3B::ORANGE);
    addChild(label,ZText,DiamondRewardTitleTag);

    LabelTTF *label2 = LabelTTF::create(__String::createWithFormat(LocalizedStringGetter::getStringByKey("AchieveRewardNum"), diamondNum)->getCString(), TITLE_FONT_NAME, TITLE_FONT_SIZE);
    label2->setAnchorPoint(Vec2::ZERO);
    label2->setPosition(REWARD_TITLE_X + label->getContentSize().width + 2,REWARD_TITLE_Y);
    label2->setColor(Color3B::WHITE);
    addChild(label2,ZText,DiamondRewardNumTag);

	auto diamondIcon1 = Sprite::createWithSpriteFrameName("AchieveDiamondIcon.png");
	diamondIcon1->setAnchorPoint(Vec2::ZERO);
	diamondIcon1->setPosition(REWARD_TITLE_X + label->getContentSize().width + 2 +label2->getContentSize().width +2,REWARD_TITLE_Y);
	this->addChild(diamondIcon1,ZText,DiamondRewardIconTag);
}


